package com.qhg.saas_server.controller;


import com.qhg.saas_server.empty.Result;
import com.qhg.saas_server.pojo.Roles;
import com.qhg.saas_server.service.RolesService;
import com.qhg.saas_server.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

;

/**
 *@作者:lsk
 *Controller接口
 *@Date:2023/07/13
*/
@CrossOrigin
@RestController
@RequestMapping("/RolesURL")
public class RolesController {


	@Autowired
	private RolesService rolesservice;

	/**
	 * 导出角色类型到Excel
	 */
	@GetMapping("/ExportExcel/{pageNum}/{pageSize}/{roleId}/{roleNameLike}")
	public Result ExportExcel(HttpServletResponse response,@PathVariable(value = "pageNum") Integer pageNum,
							  @PathVariable(value = "pageSize") Integer pageSize,
							  @PathVariable(value = "roleId") Integer roleId,
							  @PathVariable(value = "roleNameLike") String roleNameLike){
		Map map = new HashMap<String,Object>();
		if(pageNum != null){
			map.put("pageNum",pageNum);
			map.put("pageSize",pageSize);
		}
		if(roleId != 0){
			map.put("roleId",roleId);
		}
		if( !roleNameLike.equals("NULL")){
			map.put("roleNameLike",roleNameLike);
		}
		List<Roles> list = rolesservice.selectList(map);
		ExcelUtil.exportExcel(list,"角色信息","sheet1",Roles.class,"menu.xls",response);
		return Result.success(null);
	}


	/**
	根据RoleId查询
	*/
	@GetMapping("/selectByRoleId/{roleId}")
	public Result selectByRoleId(@PathVariable(value = "roleId") Integer roleId){
		return Result.success(rolesservice.selectByRoleId(roleId));
	}


	/**
	根据RoleId更新
	*/
	@PostMapping("/updateByRoleId")
	public Result updateByRoleId(@RequestBody Roles demo){
		return Result.success(rolesservice.updateByRoleId( demo));
	}



	/**
	根据RoleId删除
	*/
	@GetMapping("/deleteByRoleId/{roleId}")
	public Result deleteByRoleId(@PathVariable(value = "roleId") Integer roleId){
		return Result.success(rolesservice.deleteByRoleId(roleId));
	}





	/**
	 insert:(插入)
	*/
	@PostMapping("/insert")
	public Result insert(@RequestBody Roles demo){
		Result.success(rolesservice.insert(demo));
		return Result.success(demo);
	}



	/**
	 insertOrUpdate(插入或更新(根据主键或唯一索引更新，主键和唯一索引不会改变))
	*/
	@PostMapping("/insertOrUpdate")
	public Result insertOrUpdate(@RequestBody Roles demo){
		return Result.success(rolesservice.insertOrUpdate(demo));
	}



	/**
	 insertBath:(批量插入)
	*/
	@PostMapping("/insertBatch")
	public Result insertBatch(@RequestBody List<Roles> list){
		return Result.success(rolesservice.insertBatch(list));
	}



	/**
	 insertOrUpdateBatch:(批量插入或更新)
	*/
	@PostMapping("/insertOrUpdateBatch")
	public Result insertOrUpdateBatch(@RequestBody List<Roles> list){
		return Result.success(rolesservice.insertOrUpdateBatch(list));
	}



	/**
	 selectList:(根据参数查询集合)
	*/
	@PostMapping("/selectList")
	public Result selectList(@RequestBody Map<String,Object> param){
		Map map = new HashMap();
		if(param.get("pageNum") != null){
			map.put("total",rolesservice.selectCount(param));
		}
		map.put("data",rolesservice.selectList(param));
		return Result.success(map);
	}



	/**
	 selectCount:(根据集合查询数量)
	*/
	@PostMapping("/selectCount")
	public Result selectCount(@RequestBody Map<String,Object> param){
		return Result.success(rolesservice.selectCount(param));
	}



}